var defects = [];
var stationId;
var table;
var station = undefined;
function init(){
	$('#start').val( $('#startTime').val());
	$('#end').val( $('#endTime').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView:2,
		autoclose : true,
		endDate: new Date(),
		todayHighlight:true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView:2,
		autoclose : true,
		endDate: new Date(),
		pickerPosition:'bottom-left',
		todayHighlight:true
	});
	stationId = $('#stationId').val();
	loadData();
}
function loadData(){
	var s = $('#start').val();
	var e = $('#end').val();
	$.getJSON("/rest/protectDefect/getProtectDefectByStationId/"
			+s+"/"+e+"/"+stationId,function(result){
		defects = result;
		if(!station){
			station = defects[0]['substationName'];
			$("title").html(station+"  保护缺陷及处理");
		}
		$('#tbnum').html('共'+defects.length+'条结果');
		loadHtml();
	});
}
function query(){
	loadData();
}
function loadHtml(){
	if(table){
		table.getTable().destroy();
	}
	var columns = ['电网','厂站名称','设备名称','保护型号','保护投入时间', '保护退出时间','缺陷描述','检查及处理情况'];
	var opt = MyTable.getDefaultOptions();
	opt.paging=true;
	opt.pageLength = 5;
	opt.columnDefs = [{ "width": "10%", "targets": 0 },// width为列宽，单位px或%均可；targets表示设置哪一列
	                   { "width": "10%", "targets": 1 },
	                   { "width": "13%", "targets": 2 },
	                   { "width": "14%", "targets": 3 },
	                   { "width": "14%", "targets": 4 },
	                   { "width": "19%", "targets": 5 },
	                   { "width": "20%", "targets": 6 }];
	table = new MyTable("defectTable", columns, opt);
	var datas = [];
	for(var i in defects){
		var f = defects[i];
		datas.push([
		             str(f['subControlArea']),str(f['substationName']),str(f['name']),str(f['protectType']),
		             time(f['protectInTime']),time(f['protectQuitTime']),
		             str(f['defectDescription']),str(f['linePatrolReasult'])
		             ]);
	}
	table.setDataSet(datas).show();
}
function str(v){
	return v?v:'';
}
function time(v){
	if(!v){
		return '';
	}else{
		return new Date( v.time?v.time:v).toLocaleString();
	}
}
